﻿#include "attendancelist.h"
#include "ui_attendancelist.h"
#include "msgapi.h"
#include <QList>

AttendanceList::AttendanceList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AttendanceList) {
    ui->setupUi(this);
    initList();
    initWidget();
}

AttendanceList::~AttendanceList() {
    delete ui;
}

int AttendanceList::getCurrentPage() {
    return currentPage;
}

int AttendanceList::getTotalRecord() {
    return totalRecord;
}

void AttendanceList::setTotalRecord(int totalRecord) {
    this->totalRecord = totalRecord;
}

int AttendanceList::getTotalPage() {
    return totalPage;
}

void AttendanceList::setTotalPage(int totalPage) {
    this->totalPage = totalPage;
}

QString AttendanceList::getCurrentSearchUsername() {
    return ui->searchUsernameLineEdit->text();
}

QString AttendanceList::getCurrentSearchStartTime() {
    return ui->startTimeEdit->text();
}

QString AttendanceList::getCurrentSearchEndTime() {
    return ui->endTimeEdit->text();
}

void AttendanceList::initList() {
    QStringList headerText;
    headerText << QString("ID")
               << QString("用户名")
               << QString("打卡方式")
               << QString("打卡图片")
               << QString("打卡时间");
    // 指定列数
    ui->tableWidget->setColumnCount(5);
    // 设置颜色交替显示
    ui->tableWidget->setAlternatingRowColors(true);
    // 自适应宽度
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //设置行高
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(60);
    // 设置行宽
//    ui->tableWidget->horizontalHeader()->setDefaultSectionSize(100);
    // 设置垂直头不可见
    ui->tableWidget->verticalHeader()->setVisible(false);
    // 设置表头的高度
    ui->tableWidget->horizontalHeader()->setFixedHeight(45);

    // 为表格控件添加表头
    for(int i = 0; i < ui->tableWidget->columnCount(); i++) {
        // 给每个单元格添加一个 QTableWidgetItem实例
        QTableWidgetItem *item = new  QTableWidgetItem;
        // 设置文本
        item->setText(headerText.at(i));
        // 设置表头的字体
        QFont font;
        font.setBold(true);//加粗
        item->setFont(font);

        // 将单元格设置到 表格控件上 ，并且作为 表头
        ui->tableWidget->setHorizontalHeaderItem(i, item);
    }
}

void AttendanceList::initWidget() {
    ui->pageLabel->setAlignment(Qt::AlignVCenter);
    ui->endTimeEdit->setDateTime(QDateTime::currentDateTime());
    currentPage = 1;
    totalRecord = 0;
    getAttendanceList(currentPage, ATTENDANCE_PAGE_LIST_SIZE,
                      getCurrentSearchUsername(), getCurrentSearchStartTime(), getCurrentSearchEndTime());
}

void AttendanceList::getAttendanceList(int page, int pageSize, QString currentSearchUsername, QString currentSearchStartTime, QString currentSearchEndTime) {
    MsgApi::getMsgApi()->getAttendanceInfoList(page, pageSize,
            currentSearchUsername, currentSearchStartTime, currentSearchEndTime);
}

void AttendanceList::setPageShow(int count) {
    // 设置页面情况
    int totalPage = 0;
    if(count == ATTENDANCE_PAGE_LIST_SIZE) {
        totalPage = 1;
    } else {
        totalPage = count / ATTENDANCE_PAGE_LIST_SIZE + 1;
    }
    setTotalRecord(count);
    setTotalPage(totalPage);
    ui->pageLabel->setText(QString::number(currentPage) + "/" + QString::number(totalPage));
}


void AttendanceList::showAttendanceList(QList<AInfo> &attendanceInfoList) {
    clearAttendanceList();

    // 设置页面情况
    ui->pageLabel->setText(QString::number(currentPage) + "/" + QString::number(totalPage));

    for(int i = 0; i < attendanceInfoList.size(); i++) {
        ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);

        QTableWidgetItem *itm;

        //userId
        itm = new QTableWidgetItem;
        itm->setText(attendanceInfoList.at(i).userId);
        ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 0, itm);

        //username
        itm = new QTableWidgetItem;
        itm->setText(attendanceInfoList.at(i).username);
        ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 1, itm);

        //attWay
        itm = new QTableWidgetItem;
        itm->setText(attendanceInfoList.at(i).attWay);
        ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 2, itm);

        //picPath
        QPixmap pic = QPixmap(attendanceInfoList.at(i).picPath);//添加图片
//        pic = pic.scaled(60, 80); //设置图片的大小
        QLabel *picLabel = new QLabel();//定义一个QLabel类
        picLabel->setScaledContents(true);//设置图片适应label
//        picLabel->setMaximumSize(60, 60); //设置的大小，就是单元格的大小
        picLabel->setPixmap(pic);
        ui->tableWidget->setCellWidget(ui->tableWidget->rowCount() - 1, 3, picLabel);

        //createTime
        itm = new QTableWidgetItem;
        itm->setText(attendanceInfoList.at(i).createTime);
        ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1, 4, itm);
    }
}

void AttendanceList::clearAttendanceList() {
    int rowCount = ui->tableWidget->rowCount();
    for(int i = 0; i < rowCount; ++i) {
        ui->tableWidget->cellWidget(0, 3)->close();  // 删除图片label
        ui->tableWidget->removeRow(0);
    }
}

void AttendanceList::refreshAttendanceList() {
    ui->searchUsernameLineEdit->setText("");
    QString strBuffer;
    strBuffer = "2000-01-01 00:00:00.000";
    ui->startTimeEdit->setDateTime(QDateTime::fromString(strBuffer, "yyyy-MM-dd hh:mm:ss.zzz"));
    ui->endTimeEdit->setDateTime(QDateTime::currentDateTime());
    getAttendanceList(currentPage, ATTENDANCE_PAGE_LIST_SIZE,
                      getCurrentSearchUsername(), getCurrentSearchStartTime(), getCurrentSearchEndTime());
}

void AttendanceList::on_searchButton_clicked() {
    getAttendanceList(currentPage, ATTENDANCE_PAGE_LIST_SIZE,
                      getCurrentSearchUsername(), getCurrentSearchStartTime(), getCurrentSearchEndTime());
}

void AttendanceList::on_lastPageButton_clicked() {
    if(currentPage <= 1) {
        return;
    }
    currentPage--;
    getAttendanceList(currentPage, ATTENDANCE_PAGE_LIST_SIZE,
                      getCurrentSearchUsername(), getCurrentSearchStartTime(), getCurrentSearchEndTime());
}

void AttendanceList::on_nextPageButton_clicked() {
    if(currentPage >= totalPage) {
        return;
    }
    currentPage++;
    getAttendanceList(currentPage, ATTENDANCE_PAGE_LIST_SIZE,
                      getCurrentSearchUsername(), getCurrentSearchStartTime(), getCurrentSearchEndTime());
}
